﻿using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Sharp.Infrastructure;
using Sharp.Platform.Owin;

namespace Sharp.Platform.Mvc.Bootstrap
{
    /// <summary>
    /// LinkButton
    /// </summary>
    public sealed class LinkButtonBuilder : TagElementBuilder<LinkButtonBuilder>
    {
        #region Field


        private readonly ButtonSizeConfiguration _buttonSizeConfiguration;
        private readonly ButtonStyleConfiguration _buttonStyleConfiguration;
        private string _href = "#";

        #endregion Field

        #region Constructor

        public LinkButtonBuilder()
        {
            _buttonSizeConfiguration = new ButtonSizeConfiguration(this);
            _buttonStyleConfiguration = new ButtonStyleConfiguration(this);
            AddClass("btn").Style().Default();

        }

        #endregion Constructor

        #region Public Method

        public LinkButtonBuilder Title(string title)
        {
            TagBuilder.InnerHtml = (title);
            return this;
        }


        public ButtonSizeConfiguration Size()
        {
            return _buttonSizeConfiguration;
        }

        public LinkButtonBuilder Active(bool? active = null)
        {
            if (active.HasValue)
                return active.Value ? AddClass("active") : RemoveClass("active");

            return ToggleClass("active");
        }

        public LinkButtonBuilder Disabled(bool disabled = true)
        {
            if (disabled)
                return Attribute("disabled", "disabled", true);
            TagBuilder.Attributes.Remove("disabled");
            return this;
        }

        public LinkButtonBuilder Toggle()
        {
            return Attribute("data-toggle", "button", true);
        }

        public ButtonStyleConfiguration Style()
        {
            return _buttonStyleConfiguration;
        }

        public LinkButtonBuilder Href(string href)
        {
            _href = href;
            return this;
        }
        /// <summary>
        /// 客户端点击事件
        /// </summary>
        /// <param name="click"></param>
        /// <returns></returns>
        public LinkButtonBuilder Click(string click)
        {
            return Attribute("onclick", click, true);
        }

        #endregion Public Method

        #region Overrides of TagElementBuilder<LinkButtonBuilder>

        public override string TagName => "a";

        /// <summary>
        /// 返回 HTML 编码的字符串。
        /// </summary>
        /// <returns>
        /// HTML 编码的字符串。
        /// </returns>
        public override string ToHtmlString()
        {
            TagBuilder.AddCssClass(_buttonStyleConfiguration.ToString());
            if (string.IsNullOrWhiteSpace(_href))
                return TagBuilder.ToString();
            if (TagBuilder.Attributes.ContainsKey("onclick"))
            {
                Attribute("href", "javascript:void(0)", true);
            }
            else
            {
                Attribute("href", _href, true);
            }
            var builder = new StringBuilder();
            builder.AppendLine(TagBuilder.ToString(TagRenderMode.StartTag).Replace(TagName, "a"));
            builder.AppendLine(TagBuilder.InnerHtml);
            builder.AppendLine(TagBuilder.ToString(TagRenderMode.EndTag).Replace(TagName, "a"));

            var systemCode = ContactContext.Current.SystemCode;
            if (this.TagBuilder.Attributes.ContainsKey("data-route"))
            {
                var route = this.TagBuilder.Attributes["data-route"];
                if (Platform.AllPermission().Any(x => x.SystemCode == systemCode && x.Route == route))
                {
                    var list = SharpUserManager.Current.Permissions.Select(x => x.Route);
                    if (list.All(x => x != route))
                    {
                        return "";
                    }
                }
            }
            if (this.TagBuilder.Attributes.ContainsKey("data-permission"))
            {
                var permission = this.TagBuilder.Attributes["data-permission"];
                if (Platform.AllPermission().Any(x => x.SystemCode == systemCode && x.Code == permission))
                {
                    var list = SharpUserManager.Current.Permissions.Select(x => x.Code);
                 
                    if (list.All(x => x != permission))
                    {
                        return "";
                    }
                }
            }

            return new HtmlString(builder.ToString()).ToHtmlString();
        }

        #endregion Overrides of TagElementBuilder<LinkButtonBuilder>

        #region Overrides of Object

        /// <summary>
        /// 返回表示当前 <see cref="T:System.Object"/> 的 <see cref="T:System.String"/>。
        /// </summary>
        /// <returns>
        /// <see cref="T:System.String"/>，表示当前的 <see cref="T:System.Object"/>。
        /// </returns>
        public override string ToString()
        {
            return ToHtmlString();
        }

        #endregion Overrides of Object

        #region Help Class



        public sealed class ButtonSizeConfiguration : SizeConfiguration<LinkButtonBuilder>
        {
            public ButtonSizeConfiguration(LinkButtonBuilder tagElementBuilder)
                : base("btn", tagElementBuilder)
            {
            }

            public LinkButtonBuilder ExtraSmall()
            {
                Value = "btn-xs";
                return TagElementBuilder;
            }
        }

        public sealed class ButtonStyleConfiguration : StyleConfiguration<LinkButtonBuilder>
        {
            public ButtonStyleConfiguration(LinkButtonBuilder instance)
                : base("btn", instance)
            {
            }

            public LinkButtonBuilder Link()
            {
                Value = "btn-link";

                return Instance;
            }
        }

        #endregion Help Class
    }
}